<template>
    <div>
        <div style="display: flex;justify-content: space-between;">
            <el-button size="mini" round style="width: 100px;" @click="modifyPwd">{{mpwd?'取消修改':'修改密码'}}</el-button>
            <el-button size="mini" round style="width: 100px;" @click="goError">错题记录➤</el-button>
        </div>
        <div v-show="mpwd">
            <el-input type="text" v-model="oldPwd" placeholder="旧密码"></el-input>
            <el-input type="text" v-model="newPwd" placeholder="新密码"></el-input>
            <el-button type="primary" @click="modify">提交</el-button>
        </div>
        <!--模板列表-->
        <el-card class="box-card" v-for="(v,index) in paperList" :key="index" style="">
            <div @click="enterPaper(v)" style="display: flex;justify-content: space-between;align-items: center">
                <div class="text item">
                    {{v.name}}
                    <div style="font-size: 0.8rem;">({{v.create_time}})</div>
                </div>
                <div><i class="el-icon-arrow-right"></i></div>
            </div>
        </el-card>
    </div>
</template>

<script>
    import md5 from 'md5'
    export default {
        name: "examHome",
        data(){
            return{
                mpwd: false,
                oldPwd: '',
                newPwd: '',

                questionBankId: '', // 题库id

                paperList: [],
                showPaperList: false,

                loading: null,
            }
        },
        methods:{
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 获取试卷列表
            getPaperList(pageNumber){
                this.loadopen();
                this.config.appPost('/user/testpaper/findTestPage', {
                    pageNum: pageNumber?pageNumber:1,
                }).then(res => {
                    this.loadclose();
                    if (res.data.code === '0') {
                        this.paperList = res.data.page.list;
                        this.showPaperList = true;
                    }else if(res.data.code==='-2001'){
                        this.$router.push('/examLogin');
                    } else {
                        this.$message.error(res.data.message);
                    }
                    this.$emit('done', '');
                });
            },
            // 进入试卷
            enterPaper(v){
                let currentTime = new Date().getTime();
                let startTime = Date.parse(v.start_time.replace(/-/g,"/"));
                let endTime = Date.parse(v.end_time.replace(/-/g,"/"));
                sessionStorage.setItem('examEndTime', endTime);
                // 判断考试时间段，未到时间或者超过时间均不能进入考试
                if(currentTime>=startTime&&currentTime<endTime){
                    // this.$router.push({path: '/exam', query: {questionBankId: v.id}});
                    this.$router.push({path: '/examList', query: {questionBankId: v.id}});
                }else if(currentTime>=endTime){
                    this.$message.warning('考试已经结束');
                }else if(currentTime<startTime){
                    this.$message.warning('考试未开始');
                }
            },
            // 修改密码
            modifyPwd(){
                this.mpwd = !this.mpwd;
            },
            modify(){
                this.loadopen();
                this.config.appPost('/user/base/updatePwd', {
                    pwd: md5(this.oldPwd),
                    newPwd: md5(this.newPwd),
                }).then(res=>{
                    this.loadclose();
                    if(res.data.code==='0'){
                        this.$message({
                            message: res.data.message+'，请重新登录。',
                            type: 'success'
                        });
                        this.$router.push('/examLogin')
                    }else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                })
            },
            // 进入错题记录
            goError(){
                this.$router.push('/exam-error');
            },
        },
        mounted(){
            this.getPaperList()
        }
    }
</script>

<style scoped>
    .el-input{
        margin-bottom: 1rem;
    }
    .el-button{
        margin: 1rem 0;width: 100%;
        font-size: 1rem;
        height: 38px;
    }
    .box-card{
        margin-bottom: .5rem;
    }
</style>
